#include "StdAfx.h"
#include "glModel.h"
#include "glTexture.h"
#include "Shaders.h"
#include "ResourceManage.h"
#include <string>   // standard library: C++ style strings
#include <iostream> // include this standard library when using input/output
#include <sstream>  // requires standard library
#include <fstream>  // Note this extra header requirement whenever working with files
#include <cstdlib>
#include "../Utilities/utilities.h"

using namespace std;

ResourceManage::ResourceManage(void)
{
}


ResourceManage::~ResourceManage(void)
{
}
void ResourceManage::LoalResource(){
	char*path="../Resources/Engine/ResourceManage.txt";
	char *c_temp=new char[255];
	Vector3 v_temp;
	int i_temp;
	ifstream infile;
	infile.open(path);
	if(!infile){
		cout<<"error load file: "<<path;
		return;
	}
	infile>>c_temp>>r_numModel;
	res_model=new glModel*[r_numModel];
	for(int i=0;i<r_numModel;i++){
		res_model[i]=new glModel;
		res_model[i]->path_model=new char[255];
		infile>>c_temp>>i_temp;
		infile>>c_temp>>res_model[i]->path_model;
		res_model[i]->LoadModel();
	}
	infile>>c_temp>>r_numTexture;
	res_Texture=new glTexture*[r_numTexture];
	for(int i=0;i<r_numTexture;i++)
	{
		res_Texture[i]=new glTexture;
		res_Texture[i]->path_Texture=new char[255];
		infile>>c_temp>>i_temp;
		infile>>c_temp>>res_Texture[i]->path_Texture;
		infile>>c_temp>>c_temp;
		res_Texture[i]->tiling=-1;
		if(!strcmp(c_temp,"REPEAT")){
			res_Texture[i]->tiling=0;
		}
		if(!strcmp(c_temp,"CLAMP")){
			res_Texture[i]->tiling=1;
		}
		
	}
	infile>>c_temp>>r_numcubeTexture;
	for(int i=0;i<r_numcubeTexture;i++){
		res_Texture[i]->path_CubeTexture=new char*[6];
		infile>>c_temp>>i_temp;
		infile>>c_temp;
		for(int j=0;j<6;j++){
			res_Texture[i]->path_CubeTexture[j]=new char[255];
			infile>>res_Texture[i]->path_CubeTexture[j];
		}
	}
	infile>>c_temp>>r_numShader;
	res_Shader=new Shaders[r_numShader];
	for(int i=0;i<r_numShader;i++){
		infile>>c_temp>>i_temp;
		res_Shader[i].fileVS=new char[255];
		res_Shader[i].fileFS=new char[255];
		infile>>c_temp>>res_Shader[i].fileVS;
		infile>>res_Shader[i].fileFS;
		infile>>c_temp>>i_temp;
		for(int j=0;j<i_temp;j++){
			res_Shader[i].culling=false;
			res_Shader[i].depth_Test=false;
			res_Shader[i].alpha=false;
		}
		for(int j=0;j<i_temp;j++){
			infile>>c_temp>>c_temp;
			if(!strcmp(c_temp,"CULLING"))
				res_Shader[i].culling=true;
			if(!strcmp(c_temp,"DEPTH_TEST"))
				res_Shader[i].depth_Test=true;
			if(!strcmp(c_temp,"ALPHA"))
				res_Shader[i].alpha=true;
		}
	}
	delete c_temp;
}
void ResourceManage::CleanUp(){
	for(int i=0;i<r_numcubeTexture;i++){
		for(int j=0;j<6;j++){
			delete res_Texture[i]->path_CubeTexture[j];
		}
		delete res_Texture[i]->path_CubeTexture;
	}
	for(int i=0;i<r_numTexture;i++){
		delete res_Texture[i]->path_Texture;
		delete res_Texture[i];
	}
	for(int i=0;i<r_numModel;i++){
		res_model[i]->CleanUp();
		delete res_model[i];
	}
	for(int i=0;i<r_numShader;i++){
		res_Shader[i].CleanUp();
	}
	delete []res_model;
	delete []res_Texture;
	delete []res_Shader;
}
